<?php 
/**
 *  跑车商争议数据
 */
date_default_timezone_set("Asia/Shanghai");

set_time_limit(0);

if (isset ( $_SERVER ['REQUEST_URI'] )) {
    die ( 'error' );
}
if (! defined ( 'API_ROOT_PATH' )) {
    define ( 'API_ROOT_PATH', dirname ( __FILE__ ) );
}

require_once (API_ROOT_PATH . '/../inc/Config.php');
require_once (API_ROOT_PATH . '/../inc/dblink.php');
require_once (API_ROOT_PATH . '/../inc/Mysql.php');

$plate_prefix = array(
		     	'1' => '京',
		        '2' => '冀',
		        '3' => '津',
		        '4' => '晋',
		        '5' => '蒙',
		        '6' => '辽',
		        '7' => '吉',
		        '8' => '黑',
		        '9' => '豫',
		        '10' => '鲁',
		        '11' => '苏',
		        '12' => '渝',
		        '13' => '粤',
		        '14' => '浙',
		        '15' => '贵',
		        '16' => '川',
		        '17' => '闽',
		        '18' => '皖',
		        '19' => '桂',
		        '20' => '琼',
		        '21' => '宁',
		        '22' => '沪',
		        '23' => '甘',
		        '24' => '陕',
		        '25' => '青',
		        '26' => '鄂',
		        '27' => '湘',
		        '28' => '赣',
		        '29' => '云',
		        '30' => '新',
		        '31' => '藏',
		        '32' => '港',
		        '33' => '澳'
		    );

$db = new Mysql ( MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE );

//找到已处理的争议数据
$sql = "select * from au_dealer_issue where status = 1 ";

$history_dispute_info = $db ->fetchAll($sql);

foreach ($history_dispute_info as $key => $value) {

	$order_info_sql = "select * from au_order where order_id = ".$value['order_id'];

	$orderInfo = $db ->fetchOne($order_info_sql);

	$purchase_order_sql = "select * from au_purchase_order as po,au_order as o,au_cars as c where o.car_id = c.car_id and c.sid = po.sid and o.order_id = ".$value['order_id'];

	$poInfo = $db ->fetchOne($purchase_order_sql);

	$car_sql = "select * from au_cars where car_id = ".$orderInfo['car_id'];

	$carInfo = $db ->fetchOne($car_sql);

	$card_no = '';

	$bank_name = '';

	$account_name = '';

	if ($carInfo['car_source'] == 1) {
		
		$f_sql = "select * from au_pay_log as pl,au_fours_bank_account as fba where pl.fid = fba.id and pl.order_id =".$value['order_id'];

		$f_info = $db ->fetchOne($f_sql);

		if (!$f_info) {
			
			$four_sql = "select f.bank_name,f.card_no,f.account_name from au_cars as c ,au_car_owner as co ,au_fours as f where c.owner_id = co.owner_id and f.channel_id = co.comefrom and c.car_id = ".$carInfo['car_id'];

			$f_info = $db ->fetchOne($four_sql);

		}

		$card_no = $f_info['card_no'];

		$bank_name = $f_info['bank_name'];

		$account_name = $f_info['account_name'];

	}else{

		$signature_sql = "select * from au_signature_log where order_id = ".$value['order_id'];

		$signature_info = $db ->fetchOne($signature_sql);

		$card_no = $signature_info['payee_account'];

		$bank_name = $signature_info['deposit_bank'];

		$account_name = $signature_info['payee_name'];

	}

	$car_plate_number = $plate_prefix[$carInfo['plate_prefix']].$carInfo['plate_suffix'];

    switch ($value['issue_type']) {
    	case '1':
    		$dispute_reason = 1;
    		$order_status = 7;
    		break;
    	case '2':
    		$dispute_reason = 1;
    		$order_status = 8;
    		break;
    	case '3':
    		$dispute_reason = 2;
    		$order_status = 9;
    		break;
    	case '4':
    		$dispute_reason = 1;
    		$order_status = 8;
    		break;
    	case '5':
    		$dispute_reason = 2;
    		$order_status = 10;
    		break;
    	case '6':
    		$dispute_reason = 1;
    		$order_status = 9;
    		break;
    	case '7':
    		$dispute_reason = 1;
    		$order_status = 9;
    		break;
    	default:
    		# code...
    		break;
    }

    $order_no = $orderInfo['order_no'];

    $po_id	= $poInfo['id'];

    $po_no = $poInfo['purchase_order_nu'];

    $od_id = $value['dealer_id'];

    $od_type = 1;

    $remark = $value['remark'];

    $createtime = $value['createtime'];

    $audittime = $value['updatetime'];

    $emp_id = $value['emp_id'];

    $status = 2;

    $auditing_result = 1;

    $d_sql = "INSERT INTO au_dispute (`order_id`,`order_no`,`po_id`,`po_no`,`car_plate_number`,`od_id`,`od_type`,`remark`,`dispute_reason`,`emp_id`,`status`,`auditing_result`,`createtime`,`audittime`,`order_status`) VALUES ('".$value['order_id']."','".$order_no."','".$po_id."','".$po_no."','".$car_plate_number."','".$od_id."','".$od_type."','".$remark."','".$dispute_reason."','".$emp_id."','".$status."','".$auditing_result."','".$createtime."','".$audittime."','".$order_status."')";
	$res_did = $db ->query($d_sql);

	$did = $db ->insertId();

	if ($did > 0) {
		print_r("争议($did)生成成功\n");
	}
    //生成变更单
    $source = 7;

    $sid = $carInfo['sid'] ? $carInfo['sid'] : 0;

    $modify_no = genBillsNo('BG',intval(date('Ymd',strtotime($value['updatetime']))), '1');

    $modify_reason = 1;

    $modify_result = 1;

    $modify_status = 1;

    $originator = $emp_id;

    $createtime = $value['updatetime'];

    $dealtime = $value['updatetime'];

    $dealer_compensate = $value['cut_down_price'];   

	$m_sql = "INSERT INTO au_modify_list (`modify_no`,`order_id`,`order_no`,`po_id`,`po_no`,`sid`,`car_plate_number`,`did`,`source`,`modify_reason`,`modify_result`,`status`,`originator`,`createtime`,`dealtime`,`dealer_compensate`) VALUES ('".$modify_no."','".$value['order_id']."','".$order_no."','".$po_id."','".$po_no."','".$sid."','".$car_plate_number."','".$did."','".$source."','".$modify_reason."','".$modify_result."','".$modify_status."','".$originator."','".$createtime."','".$dealtime."','".$dealer_compensate."')";
	$res_mid = $db ->query($m_sql);

	$mid = $db ->insertId();

	if ($mid > 0) {
		print_r("变更单($mid)生成成功\n");
	}

	$modifylist_sql1 = "INSERT INTO au_modifylist_operation_log (`mid`,`emp_id`,`desc`,`createtime`) VALUES ('".$mid."','".$emp_id."','1','".$createtime."')";

	$res_modifylist_sql1 = $db ->query($modifylist_sql1);

	$modifylist_sql2 = "INSERT INTO au_modifylist_operation_log (`mid`,`emp_id`,`desc`,`createtime`) VALUES ('".$mid."','".$emp_id."','2','".$createtime."')";

	$res_modifylist_sql2 = $db ->query($modifylist_sql2);

	$dealer_compensate_sql = "select * from au_dealer_compensate where issue_id = ".$value['issue_id'];

	$dealer_compensate_info = $db ->fetchOne($dealer_compensate_sql);
	
	if ($dealer_compensate_info) {

		if ($dealer_compensate_info['compensate_amount'] > 0) {
				
			$pay_type = 5;

			$payment_no = genBillsNo('FK',intval(date('Ymd',strtotime($dealer_compensate_info['createtime']))), $pay_type);

			$pay_way = 1;

			$amount = $dealer_compensate_info['compensate_amount'];

			$create_time = $dealer_compensate_info['createtime'];

			$emp_id = 0;

			$emp_name = ' ';

			$update_time = '0000-00-00 00:00:00';

			if ($dealer_compensate_info['status'] == 1) {
				$payment_status = 0;
			}
			elseif ($dealer_compensate_info['status'] == 2) {
				$payment_status = 1;
				$emp_id = $dealer_compensate_info['emp_id'];
				$emp_sql = "select real_name from au_employee where emp_id = ".$emp_id;
				$emp_info = $db ->fetchOne($emp_sql);
				$emp_name = $emp_info['real_name'];
				$update_time = $dealer_compensate_info['compensate_time'];

				$update_m_sql = "update au_modify_list set status = 2,completetime ='".$create_time."' where mid=".$mid;

				$res_um = $db ->query($update_m_sql);

				$modifylist_sql3 = "INSERT INTO au_modifylist_operation_log (`mid`,`emp_id`,`desc`,`createtime`) VALUES ('".$mid."','".$emp_id."','3','".$create_time."')";

				$res_modifylist_sql3 = $db ->query($modifylist_sql3);
			}
			elseif ($dealer_compensate_info['status'] == 3) {
				$payment_status = 3;
				$update_m_sql = "update au_modify_list set status = 4,canceltime ='".$create_time."' where mid=".$mid;
				$res_um = $db ->query($update_m_sql);
				$update_d_sql = "update au_dispute set status = 4,canceltime='".$create_time."' where did=".$did;
				$res_ud = $db ->query($update_d_sql);

				$modifylist_sql5 = "INSERT INTO au_modifylist_operation_log (`mid`,`emp_id`,`desc`,`createtime`) VALUES ('".$mid."','".$emp_id."','5','".$create_time."')";

				$res_modifylist_sql5 = $db ->query($modifylist_sql5);
			}

			$p_sql = "INSERT INTO au_payment (`payment_no`,`status`,`pay_type`,`pay_way`,`amount`,`emp_id`,`emp_name`,`create_time`,`update_time`,`card_no`,`bank_name`,`account_name`) VALUES ('".$payment_no."','".$payment_status."','".$pay_type."','".$pay_way."','".$amount."','".$emp_id."','".$emp_name."','".$create_time."','".$update_time."','".$card_no."','".$bank_name."','".$account_name."')";

			// echo $p_sql;

			$res_pid = $db ->query($p_sql);

			$pid = $db ->insertId();

			$cp_sql = "INSERT INTO au_change_payment (`change_id`,`payment_id`,`createtime`) VALUES ('".$mid."','".$pid."','".$create_time."')";

			$cpid = $db ->query($cp_sql);
		}else{

			$update_m_sql = "update au_modify_list set status = 2,completetime ='".$create_time."' where mid=".$mid;

			$res_um = $db ->query($update_m_sql);

			$modifylist_sql3 = "INSERT INTO au_modifylist_operation_log (`mid`,`emp_id`,`desc`,`createtime`) VALUES ('".$mid."','".$emp_id."','3','".$create_time."')";

			$res_modifylist_sql3 = $db ->query($modifylist_sql3);
		}	

	}else{
		if ($value['cut_down_price'] > 0 ) {

			$pay_type = 5;

			$payment_status = 1;

			$payment_no = genBillsNo('FK',intval(date('Ymd',strtotime($createtime))), $pay_type);

			$pay_way = 1;

			$amount = $value['cut_down_price'];

			$create_time = $createtime;

			$emp_id = 31;

			$emp_name = '系统';

			$update_time = $createtime;

			$p_sql = "INSERT INTO au_payment (`payment_no`,`status`,`pay_type`,`pay_way`,`amount`,`emp_id`,`emp_name`,`create_time`,`update_time`,`card_no`,`bank_name`,`account_name`) VALUES ('".$payment_no."','".$payment_status."','".$pay_type."','".$pay_way."','".$amount."','".$emp_id."','".$emp_name."','".$create_time."','".$update_time."','".$card_no."','".$bank_name."','".$account_name."')";

			$res_pid = $db ->query($p_sql);

			$pid = $db ->insertId();

			$cp_sql = "INSERT INTO au_change_payment (`change_id`,`payment_id`,`createtime`) VALUES ('".$mid."','".$pid."','".$create_time."')";

			$cpid = $db ->query($cp_sql);

			$update_m_sql = "update au_modify_list set status = 2,completetime ='".$createtime."' where mid=".$mid;

			$res_um = $db ->query($update_m_sql);

			$modifylist_sql3 = "INSERT INTO au_modifylist_operation_log (`mid`,`emp_id`,`desc`,`createtime`) VALUES ('".$mid."','".$emp_id."','3','".$create_time."')";

			$res_modifylist_sql3 = $db ->query($modifylist_sql3);

		}else{
			$update_m_sql = "update au_modify_list set status = 2,completetime ='".$createtime."' where mid=".$mid;

			$res_um = $db ->query($update_m_sql);

			$modifylist_sql3 = "INSERT INTO au_modifylist_operation_log (`mid`,`emp_id`,`desc`,`createtime`) VALUES ('".$mid."','".$emp_id."','3','".$create_time."')";

			$res_modifylist_sql3 = $db ->query($modifylist_sql3);
		}
	}
}

$db->close();

unset($db);  

$db = null;

/**
 * 产生固定长度的随机字符串
 * @param $len
 * @param null $chars
 * @return string
 */
function genRandomChars($len, $chars = null)
{
    if (is_null($chars)) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    }
    mt_srand(10000000 * (double)microtime());
    for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
        $str .= $chars[mt_rand(0, $lc)];
    }
    return $str;
}

/**
 * 产生一个四位的随机字符串
 * @return string
 */
function gen4Chars()
{
    $strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $numbers = "0123456789";
    $randomString = genRandomChars(3);
    if (ctype_digit($randomString)) {
        $randomString = $randomString . genRandomChars(1, $strChars);
    } elseif (ctype_alpha($randomString)) {
        $randomString = $randomString . genRandomChars(1, $numbers);
    } else {
        $randomString = $randomString . genRandomChars(1);
    }
    return $randomString;
}

/**
 * 产生一个单据编号
 * @param $type
 * @param $subType
 * @return string
 */
function genBillsNo($type,$date,$subType)
{
    if ($subType < 10) $subType = "0" . $subType;
    return $type . $subType . $date . gen4Chars();
}
